/* Import the BLS capital and capital-related measures for the FHS industries
   and merge them into one file.
   Convert indexes and real variables to 1997 and 2002 bases. 
   Construct capital rental price.
   

   Output: BLS_kapital_FHS.sas7bdat
*/


%include "ASMimplibs.sas";

/* Capital income: */
PROC IMPORT OUT= WORK.ky_311_312 DATAFILE= "ky_311_312.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.ky_321 DATAFILE= "ky_321.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.ky_322 DATAFILE= "ky_322.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.ky_324 DATAFILE= "ky_324.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.ky_325 DATAFILE= "ky_325.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.ky_327 DATAFILE= "ky_327.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;

/* Productive Capital Stock (Billions of 2005 Dollars) */

PROC IMPORT OUT= WORK.pk_311_312 DATAFILE= "pk_311_312.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.pk_321 DATAFILE= "pk_321.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.pk_322 DATAFILE= "pk_322.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.pk_324 DATAFILE= "pk_324.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.pk_325 DATAFILE= "pk_325.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.pk_327 DATAFILE= "pk_327.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;

/* Capital Composition (Ratio of Capital Input to Productive Stock) */

PROC IMPORT OUT= WORK.kc_311_312 DATAFILE= "kc_311_312.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.kc_321 DATAFILE= "kc_321.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.kc_322 DATAFILE= "kc_322.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.kc_324 DATAFILE= "kc_324.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.kc_325 DATAFILE= "kc_325.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;
PROC IMPORT OUT= WORK.kc_327 DATAFILE= "kc_327.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN;

/* Depreciation Rate of Wealth Stock */

PROC IMPORT OUT= WORK.kdpr_311_312 DATAFILE= "kdpr_311_312.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.kdpr_321 DATAFILE= "kdpr_321.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.kdpr_322 DATAFILE= "kdpr_322.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.kdpr_324 DATAFILE= "kdpr_324.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.kdpr_325 DATAFILE= "kdpr_325.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.kdpr_327 DATAFILE= "kdpr_327.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;

/* [Investment] Price Deflators */

PROC IMPORT OUT= WORK.piinv_311_312 DATAFILE= "piinv_311_312.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.piinv_321 DATAFILE= "piinv_321.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.piinv_322 DATAFILE= "piinv_322.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.piinv_324 DATAFILE= "piinv_324.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.piinv_325 DATAFILE= "piinv_325.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;
PROC IMPORT OUT= WORK.piinv_327 DATAFILE= "piinv_327.csv" DBMS=CSV REPLACE;GETNAMES=YES;DATAROW=2;RUN;

data ky_311_312; set ky_311_312; 	NAICS3='311'; run;
data kdpr_311_312; set kdpr_311_312; 	NAICS3='311'; run;
data pk_311_312; set pk_311_312; 	NAICS3='311';run;
data kc_311_312; set kc_311_312; 	NAICS3='311'; run;
data piinv_311_312; set piinv_311_312;  NAICS3='311'; run;

data ky_321; set ky_321; 	 NAICS3='321'; run;
data kdpr_321; set kdpr_321; 	 NAICS3='321'; run;
data pk_321; set pk_321; 	 NAICS3='321';run;
data kc_321; set kc_321; 	 NAICS3='321'; run;
data piinv_321; set piinv_321; 	 NAICS3='321'; run;

data ky_322; set ky_322; 	 NAICS3='322'; run;
data kdpr_322; set kdpr_322; 	 NAICS3='322'; run;
data pk_322; set pk_322; 	 NAICS3='322';run;
data kc_322; set kc_322; 	 NAICS3='322'; run;
data piinv_322; set piinv_322;   NAICS3='322'; run;

data ky_324; 	set ky_324 ; 	 NAICS3='324'; run;
data kdpr_324; 	set kdpr_324; 	 NAICS3='324'; run;
data pk_324; 	set pk_324 ; 	 NAICS3='324';run;
data kc_324; 	set kc_324 ; 	 NAICS3='324'; run;
data piinv_324; set piinv_324 ;  NAICS3='324'; run;

data ky_325; 	set ky_325 ; 	 NAICS3='325'; run;
data kdpr_325; 	set kdpr_325;	 NAICS3='325'; run;
data pk_325; 	set pk_325 ; 	 NAICS3='325'; run;
data kc_325; 	set kc_325 ; 	 NAICS3='325'; run;
data piinv_325; set piinv_325 ;  NAICS3='325'; run;

data ky_327; 	set ky_327 ; 	 NAICS3='327'; run;
data kdpr_327; 	set kdpr_327; 	 NAICS3='327'; run;
data pk_327; 	set pk_327 ; 	 NAICS3='327'; run;
data kc_327; 	set kc_327 ; 	 NAICS3='327'; run;
data piinv_327; set piinv_327 ;  NAICS3='327'; run;

/* Stack 3-digit industry datasets together. */

data kc; set kc_311_312 kc_321 kc_322 kc_324 kc_325 kc_327; run;
data piinv; set piinv_311_312 piinv_321 piinv_322 piinv_324 piinv_325 piinv_327; run;
data ky; set ky_311_312 ky_321 ky_322 ky_324 ky_325 ky_327; run;
data pk; set pk_311_312 pk_321 pk_322 pk_324 pk_325 pk_327; run;
data kdpr; set kdpr_311_312 kdpr_321 kdpr_322 kdpr_324 kdpr_325 kdpr_327; run;

/* Change base year of productive capital stock, capital composition, and price deflators to 1997. */
/* Also create a version with base year=2002. */ 

data kc_1997 (keep=All_Assets equipment structures NAICS3); 	set kc; if year=1997; run;
data piinv_1997 (keep=All_Assets equipment structures NAICS3);	set piinv; if year=1997; run;
data kc_2002 (keep=All_Assets equipment structures NAICS3); 	set kc; if year=2002; run;
data piinv_2002 (keep=All_Assets equipment structures NAICS3);	set piinv; if year=2002; run;

proc datasets library=work;
modify kc_1997;
 rename All_Assets=kc_base2005_in_1997;
 rename equipment=eqkc_base2005_in_1997;
 rename structures=stkc_base2005_in_1997;
modify piinv_1997;
 rename All_Assets=piinv_base2005_in_1997;
 rename equipment=piinve_base2005_in_1997;
 rename structures=piinvs_base2005_in_1997;
modify kc_2002;
 rename All_Assets=kc_base2005_in_2002;
 rename equipment=eqkc_base2005_in_2002;
 rename structures=stkc_base2005_in_2002;
modify piinv_2002;
 rename All_Assets=piinv_base2005_in_2002;
 rename equipment=piinve_base2005_in_2002;
 rename structures=piinvs_base2005_in_2002;
run;

proc sort data=kc_1997; by NAICS3; run;
proc sort data=piinv_1997; by NAICS3; run;
proc sort data=kc_2002; by NAICS3; run;
proc sort data=piinv_2002; by NAICS3; run;

data kcpiinv_9702;
 merge kc_1997 piinv_1997 kc_2002 piinv_2002;
 by naics3;
run;

proc datasets library=work;
modify ky;
 rename All_Assets=ky;
 rename equipment=eqky;
 rename structures=stky;
modify pk;
 rename All_Assets=pk;
 rename equipment=eqpk;
 rename structures=stpk;
modify kc;
 rename All_Assets=kc;
 rename equipment=eqkc;
 rename structures=stkc;
modify kdpr;
 rename All_Assets=kdpr;
 rename equipment=eqdpr;
 rename structures=stdpr;
modify piinv;
 rename All_Assets=piinv;
 rename equipment=piinve;
 rename structures=piinvs;
run;

proc sort data=ky; by naics3 year; run;
proc sort data=pk; by naics3 year; run;
proc sort data=kc; by naics3 year; run;
proc sort data=kdpr; by naics3 year; run;
proc sort data=piinv; by naics3 year; run;

data kapital;
 merge ky pk kc kdpr piinv;
 by naics3 year ;
run;

proc sort data=kcpiinv_9702; by naics3; run;
proc sort data=kapital; by naics3 year; run;

data kapital;
 merge kapital kcpiinv_9702;
 by naics3; 

 pk_base97 = pk*piinv_base2005_in_1997; 
 eqpk_base97 = eqpk*piinve_base2005_in_1997; 
 stpk_base97 = stpk*piinvs_base2005_in_1997;
 
 pk_base02 = pk*piinv_base2005_in_2002; 
 eqpk_base02 = eqpk*piinve_base2005_in_2002; 
 stpk_base02 = stpk*piinvs_base2005_in_2002; 

 kc_base97 = 100*kc/kc_base2005_in_1997; 
 eqkc_base97 = 100*eqkc/eqkc_base2005_in_1997; 
 stkc_base97 = 100*stkc/stkc_base2005_in_1997; 
 kc_base02 = 100*kc/kc_base2005_in_2002; 
 eqkc_base02 = 100*eqkc/eqkc_base2005_in_2002; 
 stkc_base02 = 100*stkc/stkc_base2005_in_2002;

 piinv_base97 = piinv/piinv_base2005_in_1997;
 piinve_base97 = piinve/piinve_base2005_in_1997;
 piinvs_base97 = piinvs/piinvs_base2005_in_1997;

 piinv_base02 = piinv/piinv_base2005_in_2002;
 piinve_base02 = piinve/piinve_base2005_in_2002;
 piinvs_base02 = piinvs/piinvs_base2005_in_2002;

 rkl_base97 = ky/(pk_base97*kc_base97/100);
 eqrkl_base97 = eqky/(eqpk_base97*eqkc_base97/100);
 strkl_base97 = stky/(stpk_base97*stkc_base97/100);

 rkl_base02 = ky/(pk_base02*kc_base02/100);
 eqrkl_base02 = eqky/(eqpk_base02*eqkc_base02/100);
 strkl_base02 = stky/(stpk_base02*stkc_base02/100);
run;


proc datasets library=work;
modify kapital;

 label rkl_base97 = 'Rental Price of Capital in Levels, All Assets, NAICS3, base year=1997';
 label eqrkl_base97 = 'Rental Price of Capital in Levels, Equipment, NAICS3, base year=1997';
 label strkl_base97 = 'Rental Price of Capital in Levels, Structures, NAICS3, base year=1997';
 label rkl_base02 = 'Rental Price of Capital in Levels, All Assets, NAICS3, base year=2002';
 label eqrkl_base02 = 'Rental Price of Capital in Levels, Equipment, NAICS3, base year=2002';
 label strkl_base02 = 'Rental Price of Capital in Levels, Structures, NAICS3, base year=2002';

 label ky='Capital Income (Billions of nominal Dollars), All Assets, NAICS3';
 label eqky='Equipment Capital Income (Billions of nominal Dollars), NAICS3';
 label stky='Structures Capital Income (Billions of nominal Dollars), NAICS3';
 label pk_base97 = 'Productive Capital Stock (Billions of 1997 Dollars), All Assets, NAICS3';
 label eqpk_base97 = 'Equipment Productive Capital Stock (Billions of 1997 Dollars), NAICS3';
 label stpk_base97 = 'Structures Productive Capital Stock (Billions of 1997 Dollars), NAICS3';
 label pk_base02 = 'Productive Capital Stock (Billions of 2002 Dollars), All Assets, NAICS3';
 label eqpk_base02 = 'Equipment Productive Capital Stock (Billions of 2002 Dollars), NAICS3';
 label stpk_base02 = 'Structures Productive Capital Stock (Billions of 2002 Dollars), NAICS3';

 label kc_base97 = 'Capital Composition (Ratio of Capital Input to Productive Stock), All Assets, Base year=1997, NAICS3';
 label eqkc_base97 = 'Equipment Capital Composition (Ratio of Capital Input to Productive Stock), Base year=1997, NAICS3';
 label stkc_base97 = 'Structures Capital Composition (Ratio of Capital Input to Productive Stock), Base year=1997, NAICS3';
 label kc_base02 = 'Capital Composition (Ratio of Capital Input to Productive Stock), All Assets, Base year=2002, NAICS3';
 label eqkc_base02 = 'Equipment Capital Composition (Ratio of Capital Input to Productive Stock), Base year=2002, NAICS3';
 label stkc_base02 = 'Structures Capital Composition (Ratio of Capital Input to Productive Stock), Base year=2002, NAICS3';

 label kdpr = 'Depreciation Rate for All Assets, NAICS3';
 label eqdpr = 'Equipment Depreciation Rate, NAICS3';
 label stdpr = 'Structures Depreciation Rate, NAICS3';

 label piinv_base97 = 'Investment Price Deflator, All Assets, NAICS3, base year=1997';
 label piinve_base97 = 'Equipment Investment Price Deflator, NAICS3, base year=1997';
 label piinvs_base97 = 'Structures Investment Price Deflator, NAICS3, base year=1997';
 label piinv_base02 = 'Investment Price Deflator, All Assets, NAICS3, base year=2002';
 label piinve_base02 = 'Equipment Investment Price Deflator, NAICS3, base year=2002';
 label piinvs_base02 = 'Structures Investment Price Deflator, NAICS3, base year=2002';
run;

data bls.BLS_capital_FHS (keep = year naics3 rkl_base97 eqrkl_base97 strkl_base97 rkl_base02 eqrkl_base02 strkl_base02 ky eqky stky pk_base97 eqpk_base97 stpk_base97
pk_base02 eqpk_base02 stpk_base02 kc_base97 eqkc_base97 stkc_base97 kc_base02 eqkc_base02 stkc_base02 kdpr eqdpr stdpr piinv_base97 piinve_base97 piinvs_base97 piinv_base02 piinve_base02 piinvs_base02);
 set kapital;
run;

proc contents data=bls.BLS_capital_FHS;
title1 "BLS capital variables";
run;

proc print data=bls.BLS_capital_FHS;
title1 "BLS capital variables, FHS industries, 1987-2010";
run;
